package com.yykj.tencent.im.server.service.impl;

import com.yykj.tencent.im.client.form.BatchSendSystemMsgRequest;
import com.yykj.tencent.im.client.form.SendCustomMsgRequest;
import com.yykj.tencent.im.server.bean.message.TIMCustomMsgElement;
import com.yykj.tencent.im.server.bean.message.TIMMsgElement;
import com.yykj.tencent.im.server.bean.message.TIMTextMsgElement;
import com.yykj.tencent.im.server.bean.request.BatchSendMsgRequest;
import com.yykj.tencent.im.server.bean.request.SendMsgRequest;
import com.yykj.tencent.im.server.bean.request.SendSystemMsgRequest;
import com.yykj.tencent.im.server.bean.response.SendMsgResult;
import com.yykj.tencent.im.server.core.TimUtil;
import com.yykj.tencent.im.server.service.SystemMessageService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SystemMessageServiceImpl implements SystemMessageService {

    @Value("${im.systemImId}")
    private String systemImId;
    /**
     * 版本
     */
    private final String Version = "v4";

    /**
     * 单聊消息服务名
     */
    private static final String SERVICE_NAME = "openim";

    /**
     * 单聊消息相关命令字
     */
    private static final String SEND_MSG_COMMAND = "sendmsg";
    private static final String SEND_BATCH_MSG_COMMAND = "batchsendmsg";

    /**
     * 请求接口
     */
    private final String api_url = "%s/%s/%s";


    /**
     * 发送系统消息
     */
    @SneakyThrows
    @Override
    public SendMsgResult sendSystemMsg(SendSystemMsgRequest sendSystemMsgRequest) {
        List<TIMMsgElement> msgBody = new ArrayList<>();
        TIMTextMsgElement element = new TIMTextMsgElement();
        element.setMsgType("TIMTextElem");
        element.setMsgContent(new TIMTextMsgElement.TextMsgContent(sendSystemMsgRequest.getMsgBody()));
        SendMsgRequest request = SendMsgRequest.builder()
                .fromAccount(sendSystemMsgRequest.getFromAccount())
                .toAccount(sendSystemMsgRequest.getToAccount())
                .msgRandom(TimUtil.genRandom())
                .msgBody(msgBody)
                .build();
        String api = String.format(api_url, Version, SERVICE_NAME, SEND_MSG_COMMAND);
        return TimUtil.post(api, request, SendMsgResult.class);
    }

    @SneakyThrows
    @Override
    public SendMsgResult batchSendSystemMsg(BatchSendSystemMsgRequest body) {
        List<TIMMsgElement> msgBody = new ArrayList<>();
        TIMTextMsgElement element = new TIMTextMsgElement();
        element.setMsgType("TIMTextElem");
        element.setMsgContent(new TIMTextMsgElement.TextMsgContent(body.getMsgBody()));
        BatchSendMsgRequest request = BatchSendMsgRequest.builder()
                .fromAccount(systemImId)
                .toAccount(body.getToAccounts())
                .msgRandom(TimUtil.genRandom())
                .msgBody(msgBody)
                .build();
        String api = String.format(api_url, Version, SERVICE_NAME, SEND_BATCH_MSG_COMMAND);
        return TimUtil.post(api, request, SendMsgResult.class);
    }

    @Override
    @SneakyThrows
    public SendMsgResult batchSendCustomMsg(BatchSendSystemMsgRequest body) {

        List<TIMMsgElement> msgBody = new ArrayList<>();
        TIMCustomMsgElement element = new TIMCustomMsgElement();
        element.setMsgType("TIMCustomElem");
        element.setMsgContent(new TIMCustomMsgElement.CustomMsgContent(body.getMsgBody(), "customMsg", null, "dingdong.aiff"));
        msgBody.add(element);
        BatchSendMsgRequest request = BatchSendMsgRequest.builder()
                .fromAccount(systemImId)
                .toAccount(body.getToAccounts())
                .msgRandom(TimUtil.genRandom())
                .msgBody(msgBody)
                .build();
        String api = String.format(api_url, Version, SERVICE_NAME, SEND_BATCH_MSG_COMMAND);
        return TimUtil.post(api, request, SendMsgResult.class);
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("devf3e01353ec294237baaa069dba5e269f");
        BatchSendSystemMsgRequest a = BatchSendSystemMsgRequest.builder()
                .toAccounts(strings)
                .msgBody("测试少时诵诗书所所所").build();

        SystemMessageServiceImpl systemMessageService = new SystemMessageServiceImpl();
        systemMessageService.batchSendCustomMsg(a);
    }

    @Override
    @SneakyThrows
    public List<SendMsgResult> sendCustomMsg(SendCustomMsgRequest sendCustomMsgRequest) {
        ArrayList<SendMsgResult> sendMsgResults = new ArrayList<>();
        List<String> msgBodys = sendCustomMsgRequest.getMsgBodys();
        for (String msgBodyz : msgBodys) {
            List<TIMMsgElement> msgBody = new ArrayList<>();
            TIMCustomMsgElement element = new TIMCustomMsgElement();
            element.setMsgType("TIMCustomElem");
            element.setMsgContent(new TIMCustomMsgElement.CustomMsgContent(msgBodyz, "customMsg", null, "dingdong.aiff"));
            msgBody.add(element);
            BatchSendMsgRequest request = BatchSendMsgRequest.builder()
                    .fromAccount(sendCustomMsgRequest.getFromAccount())
                    .toAccount(sendCustomMsgRequest.getToAccounts())
                    .msgRandom(TimUtil.genRandom())
                    .msgBody(msgBody)
                    .build();
            String api = String.format(api_url, Version, SERVICE_NAME, SEND_BATCH_MSG_COMMAND);
            sendMsgResults.add(TimUtil.post(api, request, SendMsgResult.class));
        }
        return sendMsgResults;
    }
}